package com.kai.xadmin.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

/**
 * 类拷贝工具类：提供静态方法实现类属性的拷贝
 * 
 * @author lx
 *
 */
public class BeanCopyUtils {

	public BeanCopyUtils() {

	}

	/**
	 * 单个对象的拷贝
	 * 
	 * @param <T>
	 * @param source
	 * @param clazz
	 * @return
	 */
	public static <T> T copyBean(Object source, Class<T> clazz) {
		// 创建目标对象
		T result = null;
		try {
			result = clazz.newInstance();
			// 实现属性拷贝
			BeanUtils.copyProperties(source, result);
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 拷贝对象列表
	 * 
	 * @param <O>
	 * @param <V>
	 * @param sources
	 * @param clazz
	 * @return
	 */
	public static <O, V> List<V> copyBeanList(List<O> sources, Class<V> clazz) {
		List<V> resultList = new ArrayList();
		for (O o : sources) {
			resultList.add(copyBean(o, clazz));
		}
		return resultList;
	}

}
